<?php

namespace addons\csmitsm\library\xcore\xcflow\utils;

use addons\csmitsm\library\xcore\xcore\utils\XcDaoUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDateUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDatabaseUtils;
use addons\csmitsm\library\xcore\xcore\utils\XcDashboardUtils;

class XcformXcchartUtils
{
    public static function getXcchartOptions($xcchart_data_dim, $xcchart_data_facts)
    {
        $dimcodes = null;
        $dimlabels = null;
        if (true) {
            $dimtype = $xcchart_data_dim['type'];
            switch ($dimtype) {
                case "sql":
                    $dims = XcDatabaseUtils::getListByPuresql($xcchart_data_dim['sql']);
                    $dimcodes = XcDaoUtils::getValuesFromList($dims, "dimcode");
                    $dimlabels = XcDaoUtils::getValuesFromList($dims, "dimlabel");
                    break;
                case "range_date":
                    $dimcodes = XcDateUtils::getDateArraysFromRangeDays($xcchart_data_dim['range_date_begin'], $xcchart_data_dim['range_date_end']);
                    $dimlabels = $dimcodes;
                    break;
            }
        }

        

        $facts = [];
        foreach ($xcchart_data_facts['values'] as $xcchart_data_fact) {
            $sql = "select * from (" . $xcchart_data_fact['sql'] . ") xcchartdata where xcchartdata.dimcode in ('" . join("','", $dimcodes) . "')";
            $datas = XcDatabaseUtils::getListByPuresql($sql);
            $facts[] = XcDashboardUtils::adjustLineData($dimcodes, $datas, "dimcode", "fact");
        }

        // remove zero dims
        if ($xcchart_data_dim['hiddenZeroLabel']) {
            [$dimlabels, $facts] = XcDashboardUtils::hiddenZeroLabel($dimlabels, $facts);
        }

        return [$dimlabels, $facts];
    }
}
